package com.time.client.application.controller;

import com.google.common.base.Preconditions;
import com.time.auth.entity.Result;
import com.time.client.application.converter.CategoryDTOConverter;
import com.time.client.application.dto.CategoryDTO;
import com.time.client.domain.entity.CategoryBO;
import com.time.client.domain.service.ClientCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * 分类展示控制层类
 *
 * @author: HuangQi
 * @date: Created in 14:41 2025/10/9
 * @description: 分类展示控制层
 */

@Slf4j
@RestController
@RequestMapping("/client/category/")
public class CategoryController {

    @Autowired
    private ClientCategoryDomainService clientCategoryDomainService;

    /**
     * 获取全部顶级分类信息
     *
     * @return 顶级分类信息列表
     */
    @PostMapping("queryPrimaryCategory")
    public Result<List<CategoryDTO>> queryPrimaryCategory() {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.CategoryController.queryPrimaryCategory");
            }

            // 调用领域层
            List<CategoryBO> categoryBOList = clientCategoryDomainService.queryPrimaryCategory();
            List<CategoryDTO> categoryDTOList = CategoryDTOConverter.INSTANCE.convertBOListToDTO(categoryBOList);

            // 返回结果
            return Result.ok(categoryDTOList);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.CategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("客户端模块获取全部顶级分类失败");
        }
    }


    /**
     * 获取指定分类的下级分类信息信息
     *
     * @param categoryDTO 父类分类信息
     * @return 下级分类信息列表
     */
    @PostMapping("queryCategoryByPrimary")
    public Result<List<CategoryDTO>> queryCategoryByPrimary(@RequestBody CategoryDTO categoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.CategoryController.queryCategoryByPrimary.dto: {}", categoryDTO);
            }
            // 参数校验
            Preconditions.checkNotNull(categoryDTO.getCategoryId(), "分类的ID不能为空");

            // 调用领域层
            CategoryBO categoryBO = CategoryDTOConverter.INSTANCE.convertDTOToBO(categoryDTO);
            List<CategoryBO> categoryBOList = clientCategoryDomainService.queryCategoryByPrimary(categoryBO);
            List<CategoryDTO> categoryDTOList = CategoryDTOConverter.INSTANCE.convertBOListToDTO(categoryBOList);

            // 返回结果
            return Result.ok(categoryDTOList);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.CategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail("客户端模块获取指定分类的下级分类失败");
        }
    }

    /**
     * 获取所有父类分类及其子分类的信息
     *
     * @return 所有父类分类及其子分类的信息
     */
    @PostMapping("queryAllCategory")
    public Result<List<CategoryDTO>> queryParentAndChildCategory() {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.CategoryController.queryParentAndChildCategory");
            }

            // 调用领域层
            List<CategoryBO> categoryBOList = clientCategoryDomainService.queryParentAndChildCategory();

            // 返回结果
            List<CategoryDTO> categoryDTOList = CategoryDTOConverter.INSTANCE.convertBOListToDTO(categoryBOList);
            return Result.ok(categoryDTOList);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.CategoryController.queryAllCategory.error:{}", e.getMessage(), e);
            return Result.fail("客户端模块获取所有父类分类及其子分类失败");
        }
    }

}
